﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class IndustryDAL : BaseDAL<IndustryDAL>
	{
		#region 默认方法
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Industry model)
		{
			var strSql=new StringBuilder();
			strSql.Append("insert into Industry(");			
            strSql.Append("Name,ParentId,IsDelete");
			strSql.Append(") values (");
            strSql.Append("@Name,@ParentId,@IsDelete");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@Name", SqlDbType.NVarChar,64) ,            
                        new SqlParameter("@ParentId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.Name;                        
            parameters[1].Value = model.ParentId;                        
            parameters[2].Value = model.IsDelete;            			var obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			return obj == null ? 0 : Convert.ToInt32(obj);
		}
				
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Industry model)
		{
			var strSql=new StringBuilder();
			strSql.Append("update Industry set ");
			                                                
            strSql.Append(" Name = @Name , ");                                    
            strSql.Append(" ParentId = @ParentId , ");                                    
            strSql.Append(" IsDelete = @IsDelete  ");            			
			strSql.Append(" where Id=@Id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@Name", SqlDbType.NVarChar,64) ,            
                        new SqlParameter("@ParentId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.Name;                        
            parameters[2].Value = model.ParentId;                        
            parameters[3].Value = model.IsDelete;                        return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
				
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("update Industry set IsDelete=1 ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			var strSql=new StringBuilder();
			strSql.Append("update Industry set IsDelete=1 ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
		}
						
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataSet GetModel(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("select Id, Name, ParentId, IsDelete  ");			
			strSql.Append("  from Industry ");
			strSql.Append(" where Id=@Id  and IsDelete=0");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;
			
			var ds = DbHelperSQL.Query(strSql.ToString(),parameters);			
			return ds.Tables[0].Rows.Count>0 ? ds : null;
		}
				
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			var strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM Industry where IsDelete=0 ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}		
		#endregion
		
		#region 扩展方法
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        public List<ViewIndustryDto> GetPagingList(int pageIndex, int pageSize, string searchKeyWord, string colOrder, ref int totalCount)
        {
            var strWhere = new StringBuilder(" IsDelete=0 ");
            var parameters = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(searchKeyWord))
            {
                strWhere.Append(" and [Name] like @keyWord");
                parameters.Add(new SqlParameter { ParameterName = "@keyWord", Value = "%" + searchKeyWord + "%" });
            }
            return GetSimpleListPage<ViewIndustryDto>(pageIndex, pageSize, "Industry t1", null, "Id, Name,(case t1.ParentId when 0 then '一级分类' else (select Name from Industry t2 where t1.ParentId=t2.Id) end) as ParentName", colOrder, strWhere.ToString(), ref totalCount, parameters.ToArray());
        }

        /// <summary>
        /// 根据关键字获得数据列表
        /// </summary>
        /// <param name="keyWord">关键字</param>
        /// <param name="parentType">0查询所有行业  1查询一级行业  2查询细分行业</param>
        public DataSet GetListByKeyWord(string keyWord, int parentType)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * FROM Industry where IsDelete=0 ");
            var parameters = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(keyWord))
            {
                strSql.Append(" and [Name] like @keyWord");
                parameters.Add(new SqlParameter { ParameterName = "@keyWord", Value = "%" + keyWord + "%" });
            }
            if (parentType == 1)
            {
                strSql.Append(" and [ParentId]=0");
            }
            else if (parentType == 2)
            {
                strSql.Append(" and [ParentId]>0");
            }
            return DbHelperSQL.Query(strSql.ToString(), parameters.ToArray());
        }
		#endregion
	}
}
